<!--
 * @Author: chenyi
 * @Date: 2020-04-28 17:09
 * @LastEditors:
 * @LastEditTime:
 * @description: H5支付询问
 -->
<template>
  <view>
    <confirm-popup ref="confirm" />
  </view>
</template>

<script>
import confirmPopup from '@/components/confirmPopup/index'
export default {
  components: {
    confirmPopup
  },
  data() {
    return {
      query: {}
    }
  },
  created() {
  },
  mounted() {
    // #ifdef H5
    this.showDialog()
    // window.history.pushState(null, null, document.URL)
    // window.addEventListener('popstate', this.onBrowserBack, false)
    // #endif
  },
  destroyed() {
    // #ifdef H5
    // 当页面销毁必须要移除这个事件，vue不刷新页面，不移除会重复执行这个事件
    // window.removeEventListener('popstate', this.onBrowserBack, false)
    // #endif
  },
  onLoad(option) {
    this.query = option
  },
  onShow() {

  },
  methods: {
    getUrl() {
      const url = window.location.href
      return url
    },
    // 询问支付结果
    showDialog() {
      this.$refs.confirm.show({
        title: '支付结果',
        content: '是否已经支付？',
        yesCb: async() => {
          this.getPayResult()
        },
        cancleCb: async() => {
          this.getPayResult()
        }
      })
    },
    async getPayResult() {
      // 检查是否付款，如果未付款，跳到订单详情
      if (this.query.orderNo) {
        const payStatus = await this.checkPay()
        if (Number(payStatus) === 1) {
          if (payStatus) {
            this.$navigateTo('/pagesOrder/orderDetail/index?orderNo=' + this.query.orderNo, '', 'redirectTo')
          }
          return
        } else {
          this.$navigateTo('/pagesOrder/orderSuccess/index?orderNo=' + this.query.orderNo, '', 'redirectTo')
        }
      }
    },
    // 验证订单是否支付
    async checkPay() {
      // 判断是否付款完成
      const res = await this.$api.orderApi.getOrderDetail({
        order_no: this.query.orderNo
      })
      return res.data.status
    },
    onBrowserBack() {
      const preUrl = JSON.parse(uni.getStorageSync('__success_return__'))
      if (preUrl && (preUrl.orderNo === this.query.orderNo)) {
        this.$navigateTo(preUrl)
      } else {
        uni.navigateBack({ delta: 2 })
      }
      // 这里写点击返回键时候的事件
      // 比如判断需求执行back()或者go(-2)或者PopupShow=false隐藏弹框
    }
  }
}
</script>
